package com.cq.ssm.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cq.ssm.exception.CustomException;
import com.cq.ssm.po.Degress;
import com.cq.ssm.po.Duty;
import com.cq.ssm.po.Job;
import com.cq.ssm.po.Nation;
import com.cq.ssm.po.Result;
import com.cq.ssm.po.StuffCustom;
import com.cq.ssm.po.StuffQueryVo;
import com.cq.ssm.po.Stuffdepartment;
import com.cq.ssm.service.DegressService;
import com.cq.ssm.service.DutyService;
import com.cq.ssm.service.NationService;
import com.cq.ssm.service.StuffService;
import com.cq.ssm.service.StuffdepartmentService;

@Controller
@RequestMapping("/stuff")
public class StuffController {

	@Autowired
	private StuffService stuffService;
	@Autowired
	private StuffdepartmentService stuffdepartmentService;
	@Autowired
	private NationService nationService;
	@Autowired
	private DutyService dutyService;
	@Autowired
	private DegressService degreeService;

//	@RequestMapping("queryStuff")
//	public ModelAndView showStuffs(StuffQueryVo sqv) throws Exception {
//		// 调用service查询员工信息
//		List<StuffCustom> list = stuffService.queryStuffs(sqv);
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("stuffs", list);
//		if(sqv.getIdStuffdepartment()!=null){
//			modelAndView.addObject("selectedDepartmentId", sqv.getIdStuffdepartment());
//		}
//		modelAndView.setViewName("stuff/stuffList");
//		return modelAndView;
//	}

	@RequestMapping("queryStuffByDepartment")
	public ModelAndView queryStuffByDepartment(HttpSession session,StuffQueryVo sqv)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		if(!session.getAttribute("userType").equals("一级用户")){
			modelAndView.setViewName("stuffinf/stuff");
			return modelAndView;
		}
		// 调用service查询员工信息
		List<StuffCustom> list = stuffService.queryStuffs(sqv);
		modelAndView.addObject("stuffs", list);
		modelAndView.addObject("idDepartment", sqv.getIdStuffdepartment());
		modelAndView.setViewName("stuff/stuffByDepartmentList");
		return modelAndView;
	}

	@RequestMapping("queryStuffByStuffId")
	public ModelAndView queryStuffByStuffId(HttpSession session,StuffQueryVo sqv) throws Exception {
		// 调用service查询员工信息
		if((!session.getAttribute("idStuff").equals(sqv.getIdStuff())&&(!session.getAttribute("userType").equals("一级用户")))){
			//非一级用户查询非本人信息
			sqv.setIdStuff((String) session.getAttribute("idStuff"));
		}
		List<StuffCustom> list = stuffService.queryStuffs(sqv);
		if(list.size()==0){
			throw new CustomException("该员工信息不存在！");
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("stuff", list.get(0));
		modelAndView.setViewName("stuffinf/stuffinformation");
		return modelAndView;
	}

	// 修改用户信息
	@RequestMapping("updateStuffInfo")
	public String updateStuffInfo(Model model,
			@RequestParam(value = "idStuff", required = true) String id,
			StuffCustom sc, MultipartFile pic_file) throws Exception {
			// 原始图片名称
			String originalFileName = pic_file.getOriginalFilename();
			// 上传图片
			if (pic_file != null && originalFileName != null
					&& originalFileName.length() > 0) {
				if(!(originalFileName.endsWith(".bmp")||originalFileName.endsWith(".png")||originalFileName.endsWith(".jpg")||originalFileName.endsWith(".JPG"))){
					StuffQueryVo sqv = new StuffQueryVo();
					sqv.setIdStuff(id);
					model.addAttribute("stuff", stuffService.queryStuffs(sqv).get(0));
					model.addAttribute("fileError", "文件格式不支持！");
					return "stuffinf/stuffinformation";
				}
				// 存储图片的物理路径
			
				String pic_path="E:\\hrphoto\\";
				// 新的图片名称
				String newFileName = UUID.randomUUID()
						+ originalFileName.substring(originalFileName
								.lastIndexOf("."));
				// 新图片
				File newFile = new File(pic_path + newFileName);
				// 将内存中的数据写入磁盘
				pic_file.transferTo(newFile);
				// 将新图片名称写到stuffCustom中
				sc.setImgsrc(newFileName);
			}
		// 调用service查询员工信息
		stuffService.updateStuffById(id, sc);
		model.addAttribute("idStuff", id);
		return "updateSuccess";
	}

	// 远调员工信息
	@RequestMapping("updateDepartmentId")
	public @ResponseBody Result queryStuffByStuffId(String idStuff) throws Exception {
		boolean flag = stuffService.updateDepartmentId(idStuff);
		Result result = new Result();
		if(flag){
			result.setMessage("远调成功！");
		}else{
			result.setMessage("远调失败！");
		}
		return result;
	}

//	// 返回部门列表页
//	@RequestMapping("department")
//	public String department() throws Exception{
//		return "stuff/department";
//	}

	// 返回劳保页面
//	@RequestMapping("labour")
//	public String labour() throws Exception{
//		return "toolkit/labour";
//	}
	
	// nation分类
	// nations表示最终将方法返回值放在request中的key
	@ModelAttribute("nations")
	public List<Nation> getNationTypes() throws Exception {
		return nationService.findAll();
	}

	// 部门分类
	// departments表示最终将方法返回值放在request中的key
	@ModelAttribute("departments")
	public List<Stuffdepartment> getDepartmentTypes() throws Exception {
		return stuffdepartmentService.findAll();
	}
	
	// 根据部门id返回职位列表
	@RequestMapping("/getJobsByIdDepartment")
	public @ResponseBody List<Job> getJobsByIdDepartment(int idDepartment) throws Exception{
		List<Job> jobs = new ArrayList<Job>();
		jobs = stuffdepartmentService.findAllJobs(idDepartment);
		return jobs;
	}
	
	//duty分类
	// duties表示最终将方法返回值放在request中的key
	@ModelAttribute("duties")
	public List<Duty> getDuties() throws Exception {
		return dutyService.findAll();
	}
	//degree分类
	// degrees表示最终将方法返回值放在request中的key
	@ModelAttribute("degress")
	public List<Degress> getDegress() throws Exception {
		return degreeService.findAll();
	}
}
